﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using FlexCell;
using YArchitech.Controls;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using HyMaterialStatistic;

namespace PlumbingMaterialStatistic
{
	internal class PlumbingEquipmentList : MEPSetDataToGridParent
	{
		public PlumbingEquipmentList(Autodesk.Revit.DB.Document doc, List<Dictionary<int, SatatisticDataInfo>> lstDicElement, string gridName, GridSetInfo gridSetInfo, ref FlexCell.Grid grid)
		{
			this.m_doc = doc;
			this.m_grid = grid;
			this.m_lstDicElems = lstDicElement;
			this.m_strGridName = gridName;
			this.m_gridSetInfo = gridSetInfo;
		}

		public bool GetStatisticGrid()
		{
			if (this.m_lstDicElems.Count <= 0 || this.m_gridSetInfo == null)
			{
				return false;
			}
			this.GetStatisticData();
			this.m_grid.Locked = false;
			if (!this.m_gridSetInfo.TitleDown)
			{
				if (!this.SetDataToGridUp())
				{
					return false;
				}
			}
			else if (!this.SetDataToGridDown())
			{
				return false;
			}
			return true;
		}

		private void GetStatisticData()
		{
			if (this.m_doc == null)
			{
				return;
			}
			ProgressBarForm progressBarForm = new ProgressBarForm(this.m_lstDicElems.Count, "正在初始化设备表中的数据", ProgressBarStyle.Blocks, true, false, false);
			progressBarForm.Show(WindowHelper.HWndRevit);
			int num = 1;
			foreach (Dictionary<int, SatatisticDataInfo> dictionary in this.m_lstDicElems)
			{
				progressBarForm.RefreshProgressBar();
				if (progressBarForm.m_stopTag)
				{
					break;
				}
				if (dictionary.Count > 0)
				{
					Dictionary<int, List<string>> dictionary2 = new Dictionary<int, List<string>>();
					foreach (KeyValuePair<int, SatatisticDataInfo> keyValuePair in dictionary)
					{
						ElementId elementId = new ElementId(keyValuePair.Key);
						Element element = this.m_doc.GetElement(elementId);
						int key = -1;
						if (this.ElemExsit(keyValuePair.Value.Name, keyValuePair.Value.ElemParm, dictionary2, out key))
						{
							string text = (Convert.ToDouble(dictionary2[key][5]) + keyValuePair.Value.Amount).ToString("0.00");
							if (text.IndexOf('.') != -1)
							{
								text = text.TrimEnd(new char[]
								{
									'0'
								});
								text = text.Trim(new char[]
								{
									'.'
								});
							}
							dictionary2[key][5] = text.ToString();
						}
						else
						{
							List<string> list = new List<string>();
							list.Insert(0, keyValuePair.Value.FilterType);
							list.Insert(1, num.ToString());
							list.Insert(2, keyValuePair.Value.Name);
							list.Insert(3, keyValuePair.Value.ElemParm);
							string item = "";
							try
							{
								if (element is FamilyInstance)
								{
									item = (element as FamilyInstance).Symbol.GetParameter("材质").AsValueString();
								}
								else
								{
									item = element.GetParameter(BuiltInParameter.RBS_PIPE_MATERIAL_PARAM).AsValueString();
								}
							}
							catch
							{
								item = "";
							}
							list.Insert(4, item);
							string text2 = keyValuePair.Value.Amount.ToString("0.00");
							if (text2.IndexOf('.') != -1)
							{
								text2 = text2.TrimEnd(new char[]
								{
									'0'
								});
								text2 = text2.Trim(new char[]
								{
									'.'
								});
							}
							list.Insert(5, text2);
							list.Insert(6, "");
							list.Insert(7, "");
							list.Insert(8, "");
							dictionary2.Add(num, list);
							num++;
						}
					}
					this.m_lstDicData.Add(dictionary2);
				}
			}
		}

		private bool ElemExsit(string name, string elemParm, Dictionary<int, List<string>> dicData, out int key)
		{
			foreach (int num in dicData.Keys)
			{
				List<string> list = dicData[num];
				if (name == list[2] && elemParm == list[3])
				{
					key = num;
					return true;
				}
			}
			key = -1;
			return false;
		}

		private bool SetDataToGridUp()
		{
			if (this.m_lstDicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Cell(1, 1).Text = this.m_strGridName;
			this.m_grid.Cell(1, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			int num = 4;
			this.m_grid.Range(num + 1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num2 = 0;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicData)
			{
				num2 += dictionary.Count;
			}
			this.m_grid.Rows = num + num2;
			int num3 = 1;
			foreach (Dictionary<int, List<string>> dictionary2 in this.m_lstDicData)
			{
				List<List<int>> list = base.GroupingDicData(2, dictionary2);
				for (int i = 0; i < list.Count; i++)
				{
					List<int> list2 = list[i];
					base.SortData(3, dictionary2, ref list2);
					foreach (int key in list2)
					{
						List<string> list3 = dictionary2[key];
						if (list3.Count > 0)
						{
							for (int j = 1; j < list3.Count; j++)
							{
								if (j == 1)
								{
									this.m_grid.Cell(num, j).Text = num3.ToString();
								}
								else if (j == 5)
								{
									this.m_grid.Cell(num, j).Text = Math.Ceiling(Convert.ToDouble(list3[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0");
								}
								else
								{
									this.m_grid.Cell(num, j).Text = list3[j];
								}
								this.m_grid.Cell(num, j).Alignment = AlignmentEnum.CenterCenter;
							}
							num++;
							num3++;
						}
					}
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(2, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int k = 1; k < this.m_grid.Rows; k++)
			{
				this.m_grid.Row(k).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private bool SetDataToGridDown()
		{
			if (this.m_lstDicData.Count <= 0 || this.m_grid == null)
			{
				YJKMessageBox.Show("没有统计数据");
				return false;
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).DeleteByRow();
			int num = 0;
			foreach (Dictionary<int, List<string>> dictionary in this.m_lstDicData)
			{
				num += dictionary.Count;
			}
			this.m_grid.Rows = num + 4;
			this.m_grid.Cols = 9;
			this.DrawTitle(num);
			int num2 = num;
			int num3 = 1;
			foreach (Dictionary<int, List<string>> dictionary2 in this.m_lstDicData)
			{
				List<List<int>> list = base.GroupingDicData(2, dictionary2);
				for (int i = 0; i < list.Count; i++)
				{
					List<int> list2 = list[i];
					base.SortData(3, dictionary2, ref list2);
					foreach (int key in list2)
					{
						List<string> list3 = dictionary2[key];
						if (list3.Count > 0)
						{
							for (int j = 1; j < list3.Count; j++)
							{
								if (j == 1)
								{
									this.m_grid.Cell(num2, j).Text = num3.ToString();
								}
								else if (j == 5)
								{
									this.m_grid.Cell(num2, j).Text = Math.Ceiling(Convert.ToDouble(list3[j]) * (1.0 + (double)this.m_gridSetInfo.Margin / 100.0)).ToString("0");
								}
								else
								{
									this.m_grid.Cell(num2, j).Text = list3[j];
								}
								this.m_grid.Cell(num2, j).Alignment = AlignmentEnum.CenterCenter;
							}
							num2--;
							num3++;
						}
					}
				}
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).WrapText = true;
			this.m_grid.Range(1, 1, num + 2, this.m_grid.Cols - 1).FontName = this.m_gridSetInfo.FontName;
			this.m_grid.Range(1, 1, num + 2, this.m_grid.Cols - 1).FontSize = this.m_gridSetInfo.FontSize;
			for (int k = 1; k < this.m_grid.Rows; k++)
			{
				this.m_grid.Row(k).AutoFit();
			}
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Outside, LineStyleEnum.Thick);
			this.m_grid.Range(1, 1, this.m_grid.Rows - 1, this.m_grid.Cols - 1).set_Borders(EdgeEnum.Inside, LineStyleEnum.Thin);
			return true;
		}

		private void DrawTitle(int nRows)
		{
			this.m_grid.Cell(nRows + 1, 1).Text = "序\r号";
			this.m_grid.Range(nRows + 1, 1, nRows + 2, 1).Merge();
			this.m_grid.Cell(nRows + 2, 2).Text = "名称";
			this.m_grid.Range(nRows + 1, 2, nRows + 2, 2).Merge();
			this.m_grid.Cell(nRows + 2, 3).Text = "型号（规格）";
			this.m_grid.Range(nRows + 1, 3, nRows + 2, 3).Merge();
			this.m_grid.Cell(nRows + 2, 4).Text = "材料";
			this.m_grid.Range(nRows + 1, 4, nRows + 2, 4).Merge();
			this.m_grid.Cell(nRows + 2, 5).Text = "件\r数";
			this.m_grid.Range(nRows + 1, 5, nRows + 2, 5).Merge();
			this.m_grid.Cell(nRows + 2, 6).Text = "重量";
			this.m_grid.Range(nRows + 2, 6, nRows + 2, 7).Merge();
			this.m_grid.Cell(nRows + 1, 6).Text = "单件";
			this.m_grid.Cell(nRows + 1, 7).Text = "合计";
			this.m_grid.Cell(nRows + 2, 8).Text = "备注";
			this.m_grid.Range(nRows + 1, 8, nRows + 2, 8).Merge();
			this.m_grid.Cell(nRows + 3, 1).Text = this.m_strGridName;
			this.m_grid.Range(nRows + 3, 1, nRows + 3, 8).Merge();
			this.m_grid.Cell(nRows + 3, 1).FontSize = this.m_gridSetInfo.TitleFontSize;
			this.m_grid.Range(nRows + 1, 1, nRows + 3, 8).Alignment = AlignmentEnum.CenterCenter;
		}

		private Document m_doc;

		private List<Dictionary<int, SatatisticDataInfo>> m_lstDicElems;

		private FlexCell.Grid m_grid;

		private List<Dictionary<int, List<string>>> m_lstDicData = new List<Dictionary<int, List<string>>>();

		private string m_strGridName = "";

		private GridSetInfo m_gridSetInfo;
	}
}
